/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PersonPanel.java
 *
 * Created on Jul 12, 2011, 10:08:14 PM
 */
package panels;

import conn.EMIS4Connection;
import dao.SexDao;
import exec.NotFoundException;
import java.sql.SQLException;
import java.util.List;
import obj.Person;
import obj.Sex;

/**
 *
 * @author jm1156
 */
public class PersonPanel extends javax.swing.JPanel {

  private Person person;

  public PersonPanel() {
    this(null);
  }

  /** Creates new form PersonPanel */
  public PersonPanel(Person person) {
    super();
    initComponents();
    prepopulateFields();
    populateFields();
    this.person = person;
  }

  public Person getPopulatedPerson() {
    if (person == null) {
      person = new Person();
    }
    person.setFirstName(m_txtFirstName.getText());
    person.setLastName(m_txtLastName.getText());
    person.setDOB(new java.sql.Date(m_dchDateOfBirth.getDate().getTime()));
    person.setEmail(m_txtEmail.getText());
    try {
      person.setSSN(Integer.parseInt(m_txtSSN.getText()));
    } catch (NumberFormatException ex) {
      //just catch
    }

    return person;
  }

  private void prepopulateFields() {
    EMIS4Connection connection = EMIS4Connection.getInstance();
    connection.connect();
    SexDao sexDao = new SexDao();
    List<Sex> sexList = null;
    try {
      sexList = sexDao.loadAll(connection.getConnection());
    } catch (SQLException ex) {
      //just catch it
    }

    for (Sex sexItem : sexList) {
      m_cmbSex.addItem(sexItem.getName());
    }
  }

  private void populateFields() {
    if (person != null) {
      m_txtFirstName.setText(person.getFirstName());
      m_txtLastName.setText(person.getLastName());
      m_txtEmail.setText(person.getEmail());
      m_dchDateOfBirth.setDate(person.getDOB());

      EMIS4Connection connection = EMIS4Connection.getInstance();
      connection.connect();

      Sex sexObject = null;
      SexDao sexDao = new SexDao();
      try {
        sexObject = sexDao.getObject(connection.getConnection(), person.getSexID());
      } catch (NotFoundException ex) {
        ex.printStackTrace();
      } catch (SQLException ex) {
        ex.printStackTrace();
      }
      m_cmbSex.setSelectedItem(sexObject);
    }
  }

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        m_txtFirstName = new javax.swing.JTextField();
        m_txtLastName = new javax.swing.JTextField();
        m_txtSSN = new javax.swing.JFormattedTextField();
        m_txtEmail = new javax.swing.JTextField();
        m_cmbSex = new javax.swing.JComboBox();
        m_dchDateOfBirth = new panels.DateChooserPanel();

        setBorder(javax.swing.BorderFactory.createEtchedBorder());
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                formMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                formMouseExited(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                formMouseReleased(evt);
            }
        });

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel1.setText("First Name:");
        jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText("Last Name:");
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText("Date of Birth:");
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText("SSN:");
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel5.setText("Email:");
        jLabel5.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel8.setText("Sex:");
        jLabel8.setName("jLabel8"); // NOI18N

        m_txtFirstName.setName("m_txtFirstName"); // NOI18N

        m_txtLastName.setName("m_txtLastName"); // NOI18N

        m_txtSSN.setName("m_txtSSN"); // NOI18N

        m_txtEmail.setName("m_txtEmail"); // NOI18N

        m_cmbSex.setName("m_cmbSex"); // NOI18N

        m_dchDateOfBirth.setName("m_dchDateOfBirth"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel5)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(m_txtEmail, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE)
                    .addComponent(m_dchDateOfBirth, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(m_txtSSN, javax.swing.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(m_cmbSex, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(m_txtLastName, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE)
                    .addComponent(m_txtFirstName, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(m_txtFirstName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(m_txtLastName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(m_txtSSN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4)
                            .addComponent(m_cmbSex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8)))
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(m_dchDateOfBirth, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(m_txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void formMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseEntered
      // TODO add your handling code here:
      //this.setBackground(Color.DARK_GRAY);
      //System.out.println("Mouse Entered");
    }//GEN-LAST:event_formMouseEntered

    private void formMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseExited
      // TODO add your handling code here:
      //this.setBackground(Color.GRAY);
      //System.out.println("Mouse Exited");
    }//GEN-LAST:event_formMouseExited

    private void formMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseReleased
      // TODO add your handling code here:
      //System.out.println("Mouse Released");
    }//GEN-LAST:event_formMouseReleased
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JComboBox m_cmbSex;
    private panels.DateChooserPanel m_dchDateOfBirth;
    private javax.swing.JTextField m_txtEmail;
    private javax.swing.JTextField m_txtFirstName;
    private javax.swing.JTextField m_txtLastName;
    private javax.swing.JFormattedTextField m_txtSSN;
    // End of variables declaration//GEN-END:variables
}
